<?php 
    namespace Admin\Controller;


    class StuController extends AdminController{
        //跳转
        public function jump()
        {
            //参1 提示信息
            //参2 URL地址
            //参3 等待时间
            // $this->success('success',U('User/index'),3);
            // $this->error('错误',U('Home/index/index'),3);
            // 重定向
            // $this->redirect('User/add','',5);
            // $this->redirect('User/add','',5,'正在前往费城');  自带方法无法跳转外链
            redirect('http://www.163.com',5,'正在前往');
        }

        //ajax
        public function doAjax()
        {
            if (!IS_AJAX) {
                $this->error('不要乱搞',U('User/add'));
                exit;
            }
            $data = M('user')->select();
            // var_dump($data);
            // echo json_encode($data);
            $this->ajaxReturn($data);

        }

        //获取URL
         public function getUrl()
         {
            echo U('index').'<br>';
            echo U('User/index').'<br>';
            echo U('Home/User/index').'<br>';
            echo U('Home/User/index@www.baidu.com').'<br>';
            echo U('Home/User/index#a=22@www.baidu.com').'<br>';
         }   
        //定义操作
        public function add()
        {
            echo 'Admin stu Add<br>';
        }
        public function _before_add()
        {
            echo 'add before<br>';
        }
        public function _after_add()
        {
            echo 'after add';
        }

        //多层控制器测试
        public function index(){
          $user = D('user');

          // $user ->where(array('id'=>12,'ppp'=>222))->select();
          // $user->table('s47_user')->select();
          // $user->table('s47_user')->alias('l')->select();
          // $user->field('id,name')->select();

          // $user->order(array('id'=>'desc'))->select();

          // $data = $user->limit(5)->select();
          // $data = $user->limit(2,10)->select();
          // $data = $user->page(3,10)->select();

          // $data = $user->field('sex,count(*)')->group('sex')->select();
          // $data = $user->field('sex,count(*)')->having('count(*)>5')->group('sex')->select();
          // $data = $user->field('user2.name u2name,user.name uname')->join('user2 on user.id=user2.pass')->select();
          // $data = $user->field('u2.name u2name,u.name uname')->table('user u,user2 u2')->where('u.id=u2.pass')->select();

          // $data = $user->field('province')->distinct(true)->select();

          // $map = array();
          // $map['id'] = 7;
          // $map['sex'] = '1';
          // $map['_logic'] = 'or';
          // $data = $user->where($map)->select();

          // $map = array();
          // $map['age'] = array('gt', '20');
          // $map['sex'] = array('eq','0');
          // $map['_logic'] = 'or';
          // $data = $user->where($map)->select();

          // $m = array();
          // $m['name|sex'] = '1';
          // $m['name&sex'] = '0';
          // $m['_logic'] = 'or';
          // $data = $user->where($m)->select();

          $map = array();
          $map['name'] = array('like','%n%');
          $map['sex'] = '1';
          $map['_logic'] = 'or';
          $whe = array();
          $whe['_complex'] = $map;
          $whe['age'] = '40';
          $data = $user->where($whe)->select();

          // $user->getByName('王中王');

          echo $user->getLastSql().'<br>';
          var_dump($data);
        }
        //控制器的定义
        public function action(){
            // $app = new UserController();
            // $app = A('User');
            // $app->index();
            R('User\index');
            
        }


        public function test()
        {
            $stu = M('user');
            //查询字段
            // $data = $stu->where('id<5')->getField('name',true);
            // $data = $stu->where('id<5')->getField('id,name,sex');
            // var_dump($data);
            // 更新.改
            // $stu->where('id=2')->setField('name','DDS');
            // M('score')->where('id=1')->setInc('score'); //增加 +1;
            // M('score')->where('id=2')->setInc('score',5);
            // M('score')->where('id=3')->setDec('score',44);
            //查询最后一条SQL语句
            // echo M('score')->getLastSql().'<br>';
            //删除多个
            // M('user')->where('id >50')->delete();
            echo M('user')->fetchSql(true)->where('id >50')->delete();
        }

    }